import { listCommunity,getCommunity } from "@/api/property/community";
import { listRoom,getRoom } from "@/api/property/room";
import { listUnit ,getUnit} from "@/api/property/unit";
import { listFloor,getFloor } from "@/api/property/floor";
import { treeselect } from "@/api/workorder/repairProject";

export const workOrderMixin = {
  data(){
    return {
      queryColumns:[
        {title: '接单人', key: 'receivingPersonName', typeCode: '', type: 'input', value: ''},
        {title: '挂单人', key: 'suspendPersonName', typeCode: '', type: 'input', value: ''},
        {title: '协办人', key: 'jointlyPersonName', typeCode: '', type: 'input', value: ''},
        {title: '内容描述', key: 'content', typeCode: '', type: 'input', value: ''},
        {title: '保修项目', key: 'warrantyProject', typeCode: '', type: 'input', value: ''},
        {title: '维修类型', key: 'repairType', typeCode: 'service_type', type: 'select', value: ''},
        {title: '投诉类别', key: 'complaintsType', typeCode: 'complaints_type', type: 'select', value: ''},
        {title: '立项判别', key: 'complaintType', typeCode: 'complaints_type', type: 'select', value: ''},
        {title: '服务区域', key: 'coverage', typeCode: 'coverage', type: 'select', value: ''},
        {title: '重要程度', key: 'degreeImportance', typeCode: 'degree_importance', type: 'select', value: ''},
      ],
      //报修项目
      warrantyProjectOptions:[],
      //小区数据字典
      communityOptions: [],
      //楼栋数据字典
      floorOptions: [],
      //单元数据字典
      unitOptions: [],
      //房屋数据字典
      roomOptions: [],
    }
  },
  created() {

  },
  methods: {
//小区列表
    getCommunityOptionsList(){
      listCommunity().then(response => {
        this.communityOptions =response.rows;
      })
    },
    //小区下楼栋列表
    getBuildingData(parentId,FLAG){
      //this.form.coverage = '10'
      if(parentId && !FLAG){
        let index_0 = this.communityOptions.findIndex(x=>x.id == parentId)
        this.form.location = this.communityOptions[index_0].communityName
      }
      listFloor({communityId:parentId ||-1}).then(res => {
        this.floorOptions = res.rows
        let index = this.floorOptions.findIndex(x=>x.id == this.form.floorId)
        if(index == -1){
          this.form.floorId = null
          this.form.unitId = null
          this.form.roomId = null
        }
      })
    },
    getUnitList(parentId,FLAG){
      //this.form.coverage = '10'
      if(parentId && !FLAG ){
        let index_0 = this.communityOptions.findIndex(x=>x.id == this.form.communityId)
        let index_1 = this.floorOptions.findIndex(x=>x.id == this.form.floorId)
        this.form.location = this.communityOptions[index_0].communityName + this.floorOptions[index_1].floorName
      }
      listUnit({floorId:parentId ||-1}).then(res => {
        this.unitOptions = res.rows
        let index = this.unitOptions.findIndex(x=>x.id == this.form.unitId)
        if(index == -1){
          this.form.unitId = null
          this.form.roomId = null
        }
      })
    },
    //单元下房间列表
    getRoomListData(parentId,FLAG){
      //this.form.coverage = '10'
      if(parentId && !FLAG){
        let index_0 = this.communityOptions.findIndex(x=>x.id == this.form.communityId)
        let index_1 = this.floorOptions.findIndex(x=>x.id == this.form.floorId)
        let index_2 = this.unitOptions.findIndex(x=>x.id == this.form.unitId)
        this.form.location = this.communityOptions[index_0].communityName + this.floorOptions[index_1].floorName+ this.unitOptions[index_2].unitName
      }
      listRoom({unitId:parentId ||-1,params:{notOwnerId:this.optionType=='ADD'?'Y':''}}).then(res => {
        this.roomOptions = res.rows
        let index = this.roomOptions.findIndex(x=>x.id == this.form.roomId)
        if(index == -1){
          this.form.roomId = null
        }
      })
    },
    changeRoom(v){
      if(v){
        //this.form.coverage = '20'
        let index_0 = this.communityOptions.findIndex(x=>x.id == this.form.communityId)
        let index_1 = this.floorOptions.findIndex(x=>x.id == this.form.floorId)
        let index_2 = this.unitOptions.findIndex(x=>x.id == this.form.unitId)
        let index_3 = this.roomOptions.findIndex(x=>x.id == this.form.roomId)
        this.form.location = this.communityOptions[index_0].communityName + this.floorOptions[index_1].floorName+ this.unitOptions[index_2].unitName+this.roomOptions[index_3].roomNum
      }else{
        //this.form.coverage = '10'
      }
    },
    initHouseInfo(){
      this.getCommunityOptionsList();

      if(this.form.communityId){
        this.getBuildingData(this.form.communityId,true)
      }
      if(this.form.floorId){
        this.getUnitList(this.form.floorId,true)
      }
      if(this.form.unitId){
        this.getRoomListData(this.form.unitId,true)
      }
    },
    //报修项目
    getWarrantyProject(){
      treeselect().then(res=>{
        this.warrantyProjectOptions = res.data
      })
    },
  }

}
